﻿using System.Collections;
using UnityEngine;

namespace BToolkit.ExeUtils
{
    public class CameraVideoSource : MonoBehaviour
    {

        public static WebCamTexture cameraTexture;

        IEnumerator Start()
        {
            yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
            bool isUser = Application.HasUserAuthorization(UserAuthorization.WebCam);
            if (isUser)
            {
                WebCamDevice[] devices = WebCamTexture.devices;
                if (devices != null && devices.Length > 0)
                {
                    int width = 400; // Config.Instance.CameraResolution;
                    cameraTexture = new WebCamTexture(devices[0].name, width, (int)(width * 3 / 4f), 30);
                    cameraTexture.Play();
                    Debug.Log("拍摄头启动成功:" + cameraTexture);
                }
            }
            else
            {
                Debug.LogError("没有使用摄像头权限");
            }
        }

        public static void Play()
        {
            if (cameraTexture != null)
            {
                cameraTexture.Play();
            }
        }

        public static void Pause()
        {
            if (cameraTexture != null)
            {
                cameraTexture.Pause();
            }
        }

    }
}